package net.sk.china.city.talk.service.impl.wechat;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import net.sk.china.city.talk.dao.wechat.WechatReplyDao;
import net.sk.china.city.talk.model.wechat.WechatReply;
import net.sk.china.city.talk.request.wechat.WechatReplyRequest;
import net.sk.china.city.talk.request.wechat.WechatReplySearchRequest;
import net.sk.china.city.talk.service.wechat.WechatReplyService;
import net.sk.china.common.exception.TalkException;
import net.sk.china.common.request.PageParamRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * WechatReplyServiceImpl 接口实现
 * +----------------------------------------------------------------------
 * // | Talk To [ 聊来改善地区金融服务环境，助力企业发展 ]
 * // +----------------------------------------------------------------------
 * // | Copyright (c) 2018-2024 聊来 All rights reserved.
 * // +----------------------------------------------------------------------
 * // | Licensed ( <a href="http://www.apache.org/licenses/LICENSE-2.0">apache</a> )
 * // +----------------------------------------------------------------------
 * // | @Author: 聊来 <18970881148@qq.com>
 * // +----------------------------------------------------------------------
 * // | DateTime: 2024/2/21 10:12
 * // +----------------------------------------------------------------------
 */

@Service
public class WechatReplyServiceImpl extends ServiceImpl<WechatReplyDao, WechatReply> implements WechatReplyService {

    @Resource
    private WechatReplyDao dao;

    /**
     * 列表
     * @param request 请求参数
     * @param pageParamRequest 分页类参数
     * @return {@code List<WechatReply>}
     */
    @Override
    public List<WechatReply> getList(WechatReplySearchRequest request, PageParamRequest pageParamRequest) {
        PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());
        LambdaQueryWrapper<WechatReply> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //类型
        if (StringUtils.isNotBlank(request.getType())) {
            lambdaQueryWrapper.eq(WechatReply::getType, request.getType());
        }
        //关键字
        if (StringUtils.isNotBlank(request.getKeywords())) {
            lambdaQueryWrapper.eq(WechatReply::getKeywords, request.getKeywords());
        }
        lambdaQueryWrapper.orderByDesc(WechatReply::getId);
        return dao.selectList(lambdaQueryWrapper);
    }

    /**
     * 新增微信关键字回复表
     * @param reply 新增参数
     * @return {@code boolean}
     */
    @Override
    public boolean create(WechatReply reply) {
        //检测重复
        WechatReply voByKeywords = getVoByKeywords(reply.getKeywords());
        if (voByKeywords != null) {
            throw new TalkException(reply.getKeywords() + "关键字已经存在");
        }
        return save(reply);
    }

    /**
     * 根据关键字查询数据
     * @param keywords 新增参数
     * @return {@code WechatReply}
     */
    @Override
    public WechatReply getVoByKeywords(String keywords) {
        //检测重复
        LambdaQueryWrapper<WechatReply> objectLambdaQueryWrapper = new LambdaQueryWrapper<>();
        objectLambdaQueryWrapper.eq(WechatReply::getKeywords, keywords);
        return dao.selectOne(objectLambdaQueryWrapper);
    }

    /**
     * 查询微信关键字回复表信息
     * @param id Integer
     * @return {@code WechatReply}
     */
    @Override
    public WechatReply getInfo(Integer id) { return getById(id); }

    /**
     * 修改微信关键字回复表
     * @param request 修改参数
     * @return {@code boolean}
     */
    @Override
    public boolean updateReply(WechatReplyRequest request) {
        WechatReply wechatReply = getInfoException(request.getId(), false);
        BeanUtils.copyProperties(request, wechatReply);
        return updateVo(wechatReply);
    }

    /**
     * 修改状态
     * @param id integer id
     * @param status boolean 状态
     * @return {@code boolean}
     */
    @Override
    public Boolean updateStatus(Integer id, Boolean status) {
        WechatReply wechatReply = getInfoException(id, false);
        wechatReply.setStatus(status);
        return updateVo(wechatReply);
    }

    /**
     * 根据关键字查询数据
     * @param id Integer id
     * @return {@code WechatReply}
     */
    private WechatReply getInfoException(Integer id, boolean isTrue) {
        //检测重复
        WechatReply info = getInfo(id);
        if (null == info) {
            throw new TalkException("没有找到相关数据");
        }

        if (!info.getStatus() && isTrue) {
            throw new TalkException("没有找到相关数据");
        }

        return info;
    }

    /**
     * 修改微信关键字回复表
     * @param wechatReply 修改参数
     * @return {@code boolean}
     */
    private boolean updateVo(WechatReply wechatReply) {
        //检测重复
        WechatReply voByKeywords = getVoByKeywords(wechatReply.getKeywords());

        if (voByKeywords != null && !wechatReply.getId().equals(voByKeywords.getId())) {
            throw new TalkException(wechatReply.getKeywords() + "关键字已经存在");
        }
        return updateById(wechatReply);
    }
}
